package com.zyl.Ecs.common.domain.device.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zyl.Ecs.common.domain.device.bean.Position;
import com.zyl.Ecs.common.utils.ObjectUtil;
import com.zyl.Ecs.common.domain.device.ext.PositionExt;
import com.zyl.Ecs.common.domain.device.ext.SensorExt;
import lombok.Builder;
import lombok.Data;

import java.util.Optional;

/**
 * @version v1.0
 * @ProjectName: Ecs
 * @Package: com.zyl.Ecs.device.domain.vo
 * @ClassName: SensorVO
 * @Description TODO
 * @Author 翟玉龙
 * @Date 2024/12/5-9:36
 */
@Data
@Builder
public class SensorVO {

    @JsonProperty(value = "id")
    private Integer id;

    @JsonProperty(value = "name")
    private String name;

    @JsonProperty(value = "positionId")
    private Integer positionId;

    @JsonProperty(value = "positionName")
    private String positionName;

    public static SensorVO toVO(SensorExt sensorExt) {
        if (ObjectUtil.isNull(sensorExt)) {
            return null;
        }
        return SensorVO.builder()
                .id(sensorExt.getId())
                .name(sensorExt.getName())
                .positionId(Optional.ofNullable(sensorExt.getPosition())
                        .map(Position::getId)
                        .orElse(null))
                .positionName(Optional.ofNullable(sensorExt.getPosition())
                        .map(PositionExt::getName)
                        .orElse(null))
                .build();
    }
}
